﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.PBKT;

namespace WindowsForms.PBKT
{
	public partial class FormTimKiemCT : Form
	{
		public FormTimKiemCT(LichThiDau ltd)
		{
			InitializeComponent();
			lichThiDau = ltd;
			dgvCT.AutoGenerateColumns = false;
		}
		public List<CauThu> dsCauThu = new List<CauThu>();
		private List<CauThu> dsDoiA = new List<CauThu>();
		private List<CauThu> dsDoiB = new List<CauThu>();
		public List<CauThu> outDSTT = null;
		public List<CauThu> dsFind = null;
		private LichThiDau lichThiDau;
		private List<DoiTuyen> dsDoiTuyen;

		private void FormTimKiemCT_Load(object sender, EventArgs e)
		{

			dgvCT.AutoGenerateColumns = false;
			if (lichThiDau != null)
			{
				dsDoiTuyen = new List<DoiTuyen>();
				dsDoiTuyen.Add(DoiTuyen.Objects.GetById(lichThiDau.MaDoi1));
				dsDoiTuyen.Add(DoiTuyen.Objects.GetById(lichThiDau.MaDoi2));
				cbbDoiTuyen.DataSource = dsDoiTuyen;
				
				dsCauThu = dsDoiA = CauThu.Objects.Find(c => c.MaDT == dsDoiTuyen[0].MaDT).ToList();
				dsDoiB = CauThu.Objects.Find(c => c.MaDT == dsDoiTuyen[1].MaDT).ToList();
				dsCauThu.InsertRange(dsCauThu.Count, dsDoiB);
			}
			else
			{
				cbbDoiTuyen.DataSource = DoiTuyen.Objects.All().ToList();
				
			}
			cbbDoiTuyen.ValueMember = "MaDT";
			cbbDoiTuyen.DisplayMember = "TenDoiTuyen";
		}

		private void btnTimKiem_Click(object sender, EventArgs e)
		{
			if (lichThiDau != null)
			{
				{
					DoiTuyen dt = cbbDoiTuyen.SelectedItem as DoiTuyen;
					dsFind = dsCauThu.FindAll(delegate(CauThu ct)
					{
						return ct.TenCauThu.Contains(txtTenCauThu.Text) && ct.MaDT == dt.MaDT;
					});
					//outDSTT = CauThu.Objects.Find(c => c.MaDT == dt.MaDT && c.TenCT == String.Format("'%{0}%", txtTenCauThu.Text)).ToList();
					dgvCT.DataSource = dsFind;
				}
			}
			else {
				DoiTuyen dt = cbbDoiTuyen.SelectedItem as DoiTuyen;
				dsFind = CauThu.Objects.Find(c => c.MaDT == dt.MaDT).ToList();
				//outDSTT = CauThu.Objects.Find(c => c.MaDT == dt.MaDT && c.TenCT == String.Format("'%{0}%", txtTenCauThu.Text)).ToList();
				dgvCT.DataSource = dsFind;
			}
		}

		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnLuu_Click(object sender, EventArgs e)
		{
			outDSTT = new List<CauThu>();
			for (int i = 0; i < dgvCT.RowCount; i++)
			{
				if (dgvCT.Rows[i].Cells[0].Value != null && dgvCT.Rows[i].Cells[0].Value.ToString().CompareTo("true")==1)
				{
					CauThu ct = CauThu.Objects.GetById(dgvCT.Rows[i].Cells[1].Value.ToString());
					outDSTT.Add(ct);
					//MessageBox.Show(dgvCT[1,i].Value.ToString());
				}
			}
			//foreach (var item in outDSTT)
			//{
			//    dgvCT.Rows.Add(null, item.MaCT, item.TenCauThu, item.MaDT, item.SoAo);
			//}
			this.Close();
		}
	}
}
